package com.example.liunanxin.lnx226ordering.model;



import com.example.liunanxin.lnx226ordering.beans.UserinfoBean;
import com.example.liunanxin.lnx226ordering.iface.UserinfoListener;
import com.example.liunanxin.lnx226ordering.iface.Userinfoiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.UserinfoService;
import com.example.liunanxin.lnx226ordering.url.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserinfoModel implements Userinfoiface {
    private Retrofit retrofit;

    public UserinfoModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getUserById(int user_id, final UserinfoListener userinfoListener) {
        UserinfoService userinfoService=retrofit.create(UserinfoService.class);
        Call<UserinfoBean> call=userinfoService.getUserById(user_id);
        call.enqueue(new Callback<UserinfoBean>() {
            @Override
            public void onResponse(Call<UserinfoBean> call, Response<UserinfoBean> response) {
                if(response.isSuccessful()&&response!=null)
                    userinfoListener.onResponse(response.body());
                else
                    userinfoListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<UserinfoBean> call, Throwable t) {
                userinfoListener.onFail(t.toString());
            }
        });
    }


}
